<?php

namespace plugin\admin\app\controller;

use app\traits\DataProcessor;
use plugin\admin\app\common\Auth;
use plugin\admin\app\common\Tree;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\ActivityCategory;
use support\Request;
use support\Response;
use plugin\admin\app\model\ExchangeCategory;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 换物管理 
 */
class ExchangeCategoryController extends Crud
{
    use DataProcessor;
    
    /**
     * @var ExchangeCategory
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new ExchangeCategory;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('exchange-category/index');
    }

    public function get(): Response
    {
        return $this->json(0, 'ok', ExchangeCategory::list());
    }

    public function tree(): Response
    {
        return $this->json(0, 'ok', ExchangeCategory::getTree());
    }

    protected function formatTree($items): Response
    {
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'name' => $item->title ?? $item->name ?? $item->id,
                'value' => (string)$item->id,
                'id' => $item->id,
                'pid' => $item->pid,
                'sort' => $item->sort,
            ];
        }
        $tree = new Tree($format_items);
        return $this->json(0, 'ok', $tree->getTree());
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('exchange-category/insert');
    }

    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            $data[$password_filed] = Util::passwordHash($data[$password_filed]);
        }

        if (!Auth::isSupperAdmin() && $this->dataLimit) {
            if (!empty($data[$this->dataLimitField])) {
                $admin_id = $data[$this->dataLimitField];
                if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                    throw new BusinessException('无数据权限');
                }
            }
        }

        $data['pid'] = intval($data['pid']);

        DataProcessor::processSwitch($data, 'need_audit');

        return $data;
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('exchange-category/update');
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        if (!Auth::isSupperAdmin() && $this->dataLimit && !empty($data[$this->dataLimitField])) {
            $admin_id = $data[$this->dataLimitField];
            if (!in_array($admin_id, Auth::getScopeAdminIds(true))) {
                throw new BusinessException('无数据权限');
            }
        }
        $password_filed = 'password';
        if (isset($data[$password_filed])) {
            // 密码为空，则不更新密码
            if ($data[$password_filed] === '') {
                unset($data[$password_filed]);
            } else {
                $data[$password_filed] = Util::passwordHash($data[$password_filed]);
            }
        }

        DataProcessor::processSwitch($data, 'need_audit');

        $data['pid'] = intval($data['pid']);

        unset($data[$primary_key]);
        return [$id, $data];
    }

}
